package com.jcx.ldzj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jcx.ldzj.entity.DictionariesType;
import com.jcx.ldzj.service.IDictionariesTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 码值类别表 前端控制器
 * </p>
 *
 * @author ld
 * @since 2021-07-19
 */
@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping("/dictionaries-type")
@Api(value = "DictionariesTypeController", tags = "DictionariesTypeController")
public class DictionariesTypeController extends ApiController {
    @Autowired
    private IDictionariesTypeService iDictionariesTypeService;
    /**
     * 返回码值类型的列表，用于下拉框
     * @return
     */
    @GetMapping("/listDictionaries")
    @ApiOperation(value = "列表", notes = "传入DictionariesType对象")
    public R<List<DictionariesType>> listDictionaries() {
        List<DictionariesType> list = iDictionariesTypeService.listByState();
        return R.ok(list);
    }
    /**
     * 列表
     * @param pageNum
     * @param pageSize
     * @param dictionariesType
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("basic:dictionariestype:page")
    @ApiOperation(value = "列表", notes = "传入DictionariesType对象")
    public R<IPage<DictionariesType>> list(Integer pageNum, Integer pageSize, DictionariesType dictionariesType) {
        QueryWrapper<DictionariesType> queryWrapper = new QueryWrapper<>(dictionariesType);
        queryWrapper.orderByDesc("update_time_");
        if(dictionariesType.getDicnarsName()!=null){
            queryWrapper.like("dicnars_name_",dictionariesType.getDicnarsName());
            dictionariesType.setDicnarsName(null);
        }
        if(dictionariesType.getDicnarsType()!=null){
            queryWrapper.like("dicnars_type_",dictionariesType.getDicnarsType());
            dictionariesType.setDicnarsType(null);
        }
        IPage<DictionariesType> page = iDictionariesTypeService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return R.ok(page);
    }

    /**
     * 新增
     * @param dictionariesType
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增", notes = "传入DictionariesType")
    @RequiresPermissions("basic:dictionariestype:add")
    public R add(@Valid @RequestBody DictionariesType dictionariesType) {
        return this.success(iDictionariesTypeService.save(dictionariesType));
    }

    /**
     * 修改
     * @param dictionariesType
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改", notes = "传入DictionariesType")
    @RequiresPermissions("basic:dictionariestype:update")
    public R edit(@Valid @RequestBody DictionariesType dictionariesType) {
        return this.success(iDictionariesTypeService.updateById(dictionariesType));
    }

    /**
     * 删除
     */
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除", notes = "传入ids")
    @RequiresPermissions("basic:dictionariestype:delete")
    public R del(@ApiParam(value = "主键集合", required = true) @PathVariable("ids") String[] ids) {
        return iDictionariesTypeService.removeType(ids);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @RequiresPermissions("basic:dictionariestype:view")
    @ApiOperation(value = "详情", notes = "传入id")
    public R<DictionariesType> view(@PathVariable("id") String id) {
        DictionariesType user = iDictionariesTypeService.getById(id);
        return R.ok(user);
    }
}
